	//**				**//
//** Entry # 2 Week 2 Begin **//
	//**				**//
// Includes Buffer.h

#include "Extra Headers.h"



struct _BUFFER * Buffer_Init()
{
	struct _BUFFER * B;
	B = (struct _BUFFER *)malloc(sizeof(struct _BUFFER));
	/*B->Data = (char**)NULL;
	B->Color = (char**)NULL;
	B->AmountChunks = (int)NULL;
	B->Size = (int)NULL;*/
	return B;
}

void Buffer_Array_Init(struct _BUFFER * B,int Size)
{
	B->Data = Buffer_Data_Init(Size);
	B->Color = Buffer_Color_Init(Size);
}

char ** Buffer_Data_Init(int Size)
{
	int i = (Size / CHUNK_SIZE);
	char ** c = (char**)malloc(sizeof(char*) * i);
	for(i = 0;i <= (Size / CHUNK_SIZE);++i)
		c[i] = (char*)malloc(sizeof(char) * CHUNK_SIZE);
	c[0][0] = '\0';
	return c;
}

char ** Buffer_Color_Init(int Size)
{
	int i = (Size / CHUNK_SIZE);
	int ci = 0,s = 0;
	char ** c = (char**)malloc(sizeof(char*) * i);
	for(i = 0;i <= (Size / CHUNK_SIZE);++i) {
		c[i] = (char*)malloc(sizeof(char) * CHUNK_SIZE);
	}
	for(i = 0;s <= Size;++i,++s)
	{c[ci][i] = 'n';ITT_CHUNK_CHECK(i,ci);}
	return c;
}

void Buffer_Free_Data(struct _BUFFER * B)
{
	int i = 0;
	for(;i < B->AmountChunks;++i)
		free(B->Data[i]);
	for(;i < B->AmountChunks;++i)
		free(B->Color[i]);
	B->AmountChunks = 0;
	B->Size = 0;
}

//Does a copy and replace method of moving the data to a new structure.
void Buffer_Add_Char(struct _BUFFER * B,char a_C,char a_Color)
{
	int Itt = 0,Chunk = 0,Size = 0,Itt2 = 0;
	struct _BUFFER * H = (struct _BUFFER*)malloc(sizeof(struct _BUFFER));
	Buffer_Array_Init(H,B->Size);
	if(!B || !B->Data)
		B = Buffer_Create(0);

	//Copies the data into a temporary arry
	for(Itt=0;B->Data[Chunk][Itt];Itt++,Size++)
	{
		H->Data[Chunk][Itt] = B->Data[Chunk][Itt];
		H->Color[Chunk][Itt] = B->Color[Chunk][Itt];
		ITT_CHUNK_CHECK(Itt,Chunk);
	}Size++;
	H->Data[Chunk][Itt] = B->Data[Chunk][Itt];
	H->Color[Chunk][Itt] = B->Color[Chunk][Itt];
	H->Size = B->Size;
	H->AmountChunks = B->AmountChunks;

	//Cleans up the old data to avoid memory leaks
	Buffer_Free_Data(B);

	//Passes the old data into the new larger array
	Buffer_Array_Init(B,Size+1);
	for(Itt = 0,Itt2 = 1,Chunk = 0;Itt2 < Size;Itt++,Itt2++)
	{
		B->Data[Chunk][Itt] = H->Data[Chunk][Itt];
		B->Color[Chunk][Itt] = H->Color[Chunk][Itt];
		ITT_CHUNK_CHECK(Itt,Chunk);
	}
	B->Data[Chunk][Itt] = a_C;
	B->Color[Chunk][Itt] = a_Color;
	if(Itt == 127)
	{Chunk++;Itt=-1;}
	B->Data[Chunk][Itt+1] = '\0';
	B->Color[Chunk][Itt+1] = 'n';
	B->Size = Size+1;
	B->AmountChunks = (((B->Size) / CHUNK_SIZE) + 1);

	//frees H's memory
	Buffer_Clear(H);
	return;
}

void Buffer_Add_CSZ(struct _BUFFER * B,char * a_CSZ,char a_Color)
{
	int i = 0;
	while(a_CSZ[i])
	{
		Buffer_Add_Char(B,a_CSZ[i++],a_Color);
	}
	Buffer_Add_Char(B,' ','n');
}

//Similar to how add works, except in reverse
void Buffer_Backspace(struct _BUFFER * B)
{
	struct _BUFFER * H;
	int i = 0,c = 0,s = 0;
	while(B->Data[c][i]){ITT_CHUNK_CHECK(i,c);i++;s++;}
	if(s > 0)
	{
		H = Buffer_Create(--s);
		c = 0;i = -1;
		while(s > 0){
			H->Data[c][i] = B->Data[c][++i];
			H->Color[c][i] = B->Color[c][i];
			ITT_CHUNK_CHECK(i,c);
			s--;}
		Buffer_Free_Data(B);
		Buffer_Array_Init(B,0);
		for(i = 0,c = 0,s = 0;s < H->Size;++s,++i)
		{
			Buffer_Add_Char(B,H->Data[c][i],H->Color[c][i]);
			ITT_CHUNK_CHECK(i,c);
		}
	}
	Buffer_Print_Whole_Buffer(B,IGNORE_POS,IGNORE_POS);
}

//Frees all the memory of a buffer safetly
void Buffer_Clear(struct _BUFFER * B)
{
	Buffer_Free_Data(B);
	_DELETE(B);
}

//Creates a new buffer where A_AmountChars is how many blank characters it has stored in it.
// If the argument is 0, the first character is the null terminator
struct _BUFFER * Buffer_Create(int A_AmountChars)
{
	struct _BUFFER * H = Buffer_Init();
	int ChunkAmount = (A_AmountChars / CHUNK_SIZE);
	int Itt = 0,Chunk = 0,AmountChars = A_AmountChars;
	Buffer_Array_Init(H,A_AmountChars);
	if(AmountChars == 0)
	{
		H->Data[0][0] = '\0';
		H->Color[0][0] = 'n';
		H->Size = 1;
		H->AmountChunks = 1;
	}
	else
	{
		for(Itt=0;AmountChars > 0;Itt++,AmountChars--)
		{
			H->Data[Chunk][Itt] = ' ';
			H->Color[Chunk][Itt] = 'n';
			ITT_CHUNK_CHECK(Itt,Chunk);
		}
		H->Data[Chunk][Itt] = '\0';
		H->Color[Chunk][Itt] = 'n';
		H->Size = A_AmountChars;
		H->AmountChunks = ChunkAmount+1;
	}
	return H;
}

//Gets a specific character
char Buffer_Get_Char(struct _BUFFER * B,int Itt,int Chunk)
{
	if(Chunk < B->AmountChunks)
		return B->Data[Chunk][Itt];
	else return '\0';
}

//Prints out the entire buffer, starts at the first [128] then moves to the next
void Buffer_Print_Whole_Buffer(struct _BUFFER * B,int x,int y)
{
	int i = 0,c = 0;
	if(x > -1 && y > -1)
		Set_Cursor_XY(x,y);
	while(Buffer_Get_Char(B,i,c))
	{
		Set_Color(B->Color[c][i]);
		printf("%c",Buffer_Get_Char(B,i,c));
		ITT_CHUNK_CHECK(i,c);i++;
	}
	Set_Color('n');
}

	//**				**//
//** Entry # 2 Week 2 End **//
	//**				**//